package net.grandnature.android.morse.player;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.R;
import android.graphics.Color;
import android.os.Handler;
import android.view.View;

public class MorseFlushPlayer implements MorsePlayer {
	View v;
	Handler handler;
	List<Boolean> seq;
	int shortTime;
	public MorseFlushPlayer(Handler handler, View v, int shortTime) {
		this.v = v;
		this.handler = handler;
		this.seq = new ArrayList<Boolean>();
		this.shortTime = shortTime;
	}
	Runnable on_ = new Runnable() {
		@Override
		public void run() {
			v.setBackgroundColor(R.color.white);
		}
	};
	Runnable off_ = new Runnable() {
		@Override
		public void run() {
			v.setBackgroundColor(R.color.black);
		}
	};
	@Override
	public void longSignal() {
		seq.add(true);
		seq.add(true);
		seq.add(true);
	}

	@Override
	public void shorSignal() {
		seq.add(true);
	}

	@Override
	public void silent(int length) {
		for (int i = 0; i < length; i++) {
			seq.add(false);
		}
	}

	@Override
	public void close() throws IOException {
		for (final Boolean b : seq) {
			handler.post(new Runnable() {
				@Override
				public void run() {
					if(b){
						v.setBackgroundColor(Color.WHITE);
					
					}else{
						v.setBackgroundColor(Color.BLACK);
					}
					v.invalidate();
				}
			});
			try {
				Thread.sleep(shortTime);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
